from PyQt5.QtWidgets import QDialog, QHBoxLayout, QApplication, QPushButton, QLabel

"""
说实话，这一节作者照顾不同的版本，写得很乱。我就用了简单直接的方法。
"""


class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        contents = ["One", "Two", "Three", "Four", "Five"]
        buttons = [QPushButton(text) for text in contents]  # 推导列表生成按钮组
        self.label = QLabel()
        layout = QHBoxLayout()
        for button in buttons:
            layout.addWidget(button)
            button.clicked.connect(self.button_click)
        layout.addWidget(self.label)
        self.setLayout(layout)

        self.setWindowTitle("Sinals and Slots")

    def button_click(self):
        button = self.sender()
        self.label.setText("You clicked button {}".format(button.text()))


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    form = Form()
    form.show()
    sys.exit(app.exec())
